#include "./res/m.h"




//空气检测显示模块
char* air_testing(lv_obj_t *win)
{
    // 创建一个按钮
    lv_obj_t *btn = lv_btn_create(win);
    lv_obj_align(btn, LV_ALIGN_RIGHT_MID, -70, 40);
    lv_obj_set_size(btn, 250, 150);

    // 创建一个样式
    static lv_style_t style;
    lv_style_init(&style);
    
    // 设置按钮背景的透明度
    lv_style_set_bg_opa(&style, LV_OPA_60);
    // 设置按钮背景的颜色
    lv_style_set_bg_color(&style, lv_palette_main(LV_PALETTE_ORANGE));
    // 将样式应用到按钮上
    lv_obj_add_style(btn, &style, LV_PART_MAIN);
    
    //创建标签显示空气质量检测信息
    lv_obj_t *label_air = lv_label_create(btn);
    // 设置用常见汉字
    lv_style_set_text_font(&style, &lv_font_simsun_16_cjk);
    lv_obj_align(label_air, LV_ALIGN_CENTER, 0, 0);


    // 向聚合数据获取空气质量信息，需要插网线
    // 创建TCP套接字文件
    int socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if(socketfd == -1)
    {
        perror("socket error");
        return NULL;
    }
 
    // 根据域名 得到 服务器的IP地址
    // 203.107.54.210   
    // struct hostent *info = gethostbyname("apis.juhe.cn");
    // if(info == NULL)
    // {
    //     perror("gethostbyname error");
    //     return;
    // }
    // char ip[100] = {0};
    // strcpy(ip, inet_ntoa(*(struct in_addr*)((info->h_addr_list)[0])));
    // printf("%s\n",ip);
    char ip[100] = {0};
    strcpy(ip, "203.107.54.210");
    printf("%s\n",ip);


    // 连接服务器   HTTP协议默认端口号都是80
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(80);
    serveraddr.sin_addr.s_addr = inet_addr(ip);
    int ret = connect(socketfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
    if(ret == -1){
        perror("perror error");
        return NULL;
    }
    printf("connect success\n");

    // 发送数据  HTTP请求报文
    char *requestHttp = "GET http://apis.juhe.cn/fapigw/air/live?cityId=189&key=42c517b019e8fc2c031ab9b96759ecd4 HTTP/1.1\r\n"    
                        "Content-Type:application/x-www-form-urlencoded\r\n"                                        
                        "Host:apis.juhe.cn\r\n\r\n";

    ret = write(socketfd, requestHttp, strlen(requestHttp));
    if(ret == -1)
    {
        perror("write error");
        return NULL;
    }

    //5.接收服务器响应报文
    char buf[1024*20] = {0};
    ret = read(socketfd, buf, sizeof(buf));
    if(ret == -1)
    {
         perror("read error");
        return NULL;
    }

    printf("%s\n",buf);

    //6.关闭文件
    close(socketfd);


    // 分离响应头和JSON部分
    // 查找响应头和正文之间的分隔符
    char *body = strstr(buf, "\r\n\r\n");
    if (body == NULL) {
        fprintf(stderr, "未找到头部和正文的分隔符。\n");
        return NULL;
    }

    // 跳过头部，指向正文开始的位置
    body += 7; // 跳过 "\r\n\r\n"

    printf("body:%s",body);

    
    //用JSON将数据中的今天的空气质量信息提取出来
    //解析json数据
    //将普通的json文本数据buf 转换成JSON对象
    cJSON *root = cJSON_Parse(body);
    if (root == NULL)
    {
        perror("root error");
        return NULL;
    }
    

    //根据键result 获取 值
    cJSON *result = cJSON_GetObjectItem(root,"result");
    //根据键CityJC 获取 值
    cJSON *CityJC = cJSON_GetObjectItem(result,"CityJC");
    printf("CityJC:%s\n",CityJC->valuestring);
    //根据键PM2_5 获取 值
    cJSON *PM2_5 = cJSON_GetObjectItem(result,"PM2_5");
    printf("PM2_5:%s\n",PM2_5->valuestring);
    //根据键Quality 获取 值
    cJSON *Quality = cJSON_GetObjectItem(result,"Quality");
    printf("Quality:%s\n",Quality->valuestring);

    char *pj_buf = malloc(1024 * sizeof(char));
    //装优字
    char Quality_buf[10]={0};
    
        if(strcmp(Quality->valuestring,"优") == 0)
        {
            strcpy(Quality_buf,"good");
        }
        else
        {
            strcpy(Quality_buf,Quality->valuestring);
        }
    
    
    // 更新标签文本
    sprintf(pj_buf,"CityJC:%s\nPM2_5:%s\nQuality:%s\n", CityJC->valuestring, PM2_5->valuestring, Quality_buf);
    lv_label_set_text(label_air, pj_buf);

    return pj_buf;
}